/********************************************************************************
* File Name:	searchrenter.cpp
* Description:	第7章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "searchrenter.h"
#include "ui_searchrenter.h"
#include <QtSql>
#include <QSqlQuery>
#include <QMessageBox>
#include "databaseconnection.h"

searchrenter::searchrenter(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::searchrenter)
{
    ui->setupUi(this);
}

searchrenter::~searchrenter()
{
    delete ui;
}

void searchrenter::on_btnall_clicked()
{

    if(!databaseconnection::Open())
    {
        QMessageBox::about(this,"Message","connect error");
        return;
    }
    else
    {
        QSqlQueryModel *model = new QSqlQueryModel;
        model->setQuery("select * from customer");
        model->setHeaderData(0,Qt::Horizontal,tr("Id"));
        model->setHeaderData(1,Qt::Horizontal,tr("Name"));
        model->setHeaderData(2,Qt::Horizontal,tr("Sex"));
        model->setHeaderData(3,Qt::Horizontal,tr("nativePlace"));
        model->setHeaderData(4,Qt::Horizontal,tr("idCard"));
        model->setHeaderData(5,Qt::Horizontal,tr("roomId"));
        model->setHeaderData(6,Qt::Horizontal,tr("inDate"));
        model->setHeaderData(7,Qt::Horizontal,tr("leaveDate"));
        model->setHeaderData(8,Qt::Horizontal,tr("contractId"));
        model->setHeaderData(9,Qt::Horizontal,tr("price"));
        model->setHeaderData(9,Qt::Horizontal,tr("remark"));
        ui->tbvclient->setModel(model);
        ui->tbvclient->show();
        databaseconnection::Close();
    }
}

void searchrenter::on_btnreset_clicked()
{
    this->close();
}

void searchrenter::on_btnsearch_clicked()
{
    if(ui->lehouseid->text().trimmed()==""&&ui->leclientid->text().trimmed()==""&&ui->lename->text().trimmed()=="")
    {
        QMessageBox::about(this,"Message","please input something!");
    }
    QString houseId = ui->lehouseid->text().trimmed();
    QString customerId = ui->leclientid->text().trimmed();
    QString Name = ui->lename->text().trimmed();
    //QString condition = "";
    QString cmd = "";
    if(houseId!="")
    {
        cmd = "select * from house.customer where roomId='"+houseId+"'";
    }
    else if(customerId!="")
    {
        cmd = "select * from house.customer where customerId='"+customerId+"'";
    }
    else
    {
        cmd = "select * from house.customer where customerName='"+Name+"'";
    }
    if(!databaseconnection::Open())
    {
        QMessageBox::about(this,"Message","connect error");
        return;
    }
    else
    {
        QSqlQueryModel *model = new QSqlQueryModel;
        model->setQuery(cmd);
        model->setHeaderData(0,Qt::Horizontal,tr("Id"));
        model->setHeaderData(1,Qt::Horizontal,tr("Name"));
        model->setHeaderData(2,Qt::Horizontal,tr("Sex"));
        model->setHeaderData(3,Qt::Horizontal,tr("nativePlace"));
        model->setHeaderData(4,Qt::Horizontal,tr("idCard"));
        model->setHeaderData(5,Qt::Horizontal,tr("roomId"));
        model->setHeaderData(6,Qt::Horizontal,tr("inDate"));
        model->setHeaderData(7,Qt::Horizontal,tr("leaveDate"));
        model->setHeaderData(8,Qt::Horizontal,tr("contractId"));
        model->setHeaderData(9,Qt::Horizontal,tr("price"));
        model->setHeaderData(9,Qt::Horizontal,tr("remark"));
        ui->tbvclient->setModel(model);
        ui->tbvclient->show();
        databaseconnection::Close();
    }
}
